<?php

/**
 * This is the model class for table "{{category}}".
 *
 * The followings are the available columns in table '{{category}}':
 * @property integer $category_id
 * @property string $name
 * @property integer $group_id
 * @property string $avatar
 * @property integer $isshow
 */
class Category extends CActiveRecord
{

    //Show type

    const HIDDEN          = 0;
    const HIDDEN_HOMEPAGE = 1;
    const SHOW_HOMEPAGE   = 2;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Category the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{category}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, group_id', 'required'),
            array('group_id, isshow', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 200),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('category_id, name, group_id,avatar, isshow', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        $rel               = parent::relations();
        $rel['group']      = array(self::BELONGS_TO, 'BusinessCategory', 'group_id');
        $rel['service']    = array(self::HAS_MANY, 'Service', 'category_id');
        $rel['serviceBiz'] = array(
            self::HAS_MANY,
            'Service',
            'category_id',
            'condition' => 'author_id=:a',
//            'params' => array(':a' => Yii::app()->controller->business->id)
        );
        return $rel;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'category_id' => Yii::t('main', 'Category'),
            'name'        => Yii::t('main', 'Name'),
            'group_id'    => Yii::t('main', 'Merchant Category'),
            'avatar'      => Yii::t('main', 'Avatar'),
            'isshow'      => Yii::t('main', 'Show Type'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('category_id', $this->category_id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('avatar', $this->avatar, true);
        $criteria->compare('isshow', $this->isshow);
        $criteria->compare('group_id', $this->group_id);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * @version 19.10.2013
     * @return array list of category n id
     */
    public static function GetAllCat($firstZero = TRUE)
    {

        $model = Category::model()->findAll(' isshow != :s', array(
            ':s' => self::HIDDEN,
        ));
        $arr   = array();
        if ($firstZero)
        {
            $arr[0] = 'Any';
        }

        foreach ($model as $cat)
        {
            $arr[$cat->category_id] = $cat->name;
        }

        return $arr;
    }

    /**
     * Using at Creating service
     * @param int $biz_id biz id
     * @return type
     */
    public static function GetAllCategory($biz_id = NULL)
    {
        $arr = array();

        if ($biz_id)
        {
            $biz_model = Merchant::getRecordById($biz_id);

            if ($biz_model->isBranch)
            {
                $biz_cate = BusinessCategoryUser::model()->findAll("biz_id={$biz_model->author->id} OR biz_id=$biz_id");
            }
            else
            {
                $biz_cate = BusinessCategoryUser::model()->findAll("biz_id=$biz_id");
            }

            $arr_cate = array();

            //Get group belong biz
            foreach ($biz_cate as $group)
            {
                $arr_cate[] = $group->category_id;
            }

            $cri   = new CDbCriteria();
            $cri->addInCondition('group_id', $arr_cate);
            $model = self::model()->findAll($cri);
        }
        else
        {
            $model = self::model()->findAll();
        }

        if ($model)
        {
            foreach ($model as $c)
            {
                $arr[] = array(
                    'id'       => $c->category_id,
                    'name_cat' => $c->name,
                    'group'    => isset($c->group) ? $c->group->name : ''
                );
            }
        }

        return $arr;
    }

    /**
     * Using at Creating service
     * @param int $biz_id biz id
     * @return type
     */
    public static function GetAllCategoryByRole()
    {
        if (user()->isMerchant())
        {
            $biz_model = Merchant::getRecordById(Yii::app()->user->id);

            if ($biz_model->isBranch)
            {
                $biz_cate = BusinessCategoryUser::model()->findAll("biz_id={$biz_model->author->id} OR biz_id=" . Yii::app()->user->id);
            }
            else
            {
                $biz_cate = BusinessCategoryUser::model()->findAll("biz_id=" . Yii::app()->user->id);
            }

            $arr_cate = array();

            //Get group belong biz
            foreach ($biz_cate as $group)
            {
                $arr_cate[] = $group->category_id;
            }

            $cri   = new CDbCriteria();
            $cri->addInCondition('group_id', $arr_cate);
            $model = self::model()->findAll($cri);
        }
        else
            $model     = self::model()->findAll();
        $arr[NULL] = '';
        if ($model)
        {
            foreach ($model as $c)
            {
                $arr[] = array(
                    'id'       => $c->category_id,
                    'name_cat' => $c->name,
                    'group'    => isset($c->group) ? $c->group->name : ''
                );
            }
        }

        return $arr;
    }

    public static function getListShowType()
    {
        return array(
            self::HIDDEN          => Yii::t('main', 'Hidden'),
            self::HIDDEN_HOMEPAGE => Yii::t('main', 'Hidden in Homepage'),
            self::SHOW_HOMEPAGE   => Yii::t('main', 'Show in Homepage'),
        );
    }

    public function getShowType()
    {
        $arr = self::getListShowType();
        return $arr[$this->isshow];
    }

    public function getPublicAvatar()
    {
        if ($this->avatar)
            return Yii::app()->baseUrl . "/" . $this->avatar;
        else
            return Yii::app()->controller->assetDir . '/img/no-image.png';
    }

    /**
     * @return string url service's detail page at homepage
     */
    public function getDetailUrl()
    {
        return Yii::app()->createUrl('frontend/search/index', array('cat' => $this->category_id));
    }

    public function getEditUrl()
    {
        return Yii::app()->createUrl('category/edit', array('pk' => $this->category_id));
    }

}
